import insert_sql
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
username = ['']
def zhangzhongyun_novel():
    for s in range(0,2):
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        # driver = webdriver.Chrome(r'D:\Program Files\chromedriver.exe')
        url = "https://inovel.818tu.com/backend/referral_links/index"
        driver.get('{}'.format(url))
        driver.maximize_window()
        driver.implicitly_wait(5)
        driver.find_element_by_id('username').clear()
        driver.find_element_by_id('username').send_keys('{0}'.format(username[s]))
        #定位密码
  
        driver.find_element_by_name('password').clear()
        driver.find_element_by_name('password').send_keys('{0}'.format(''))
        #点击登录
        driver.find_element_by_id('loginbtn').click()
        # 获取页面源码
        # body = driver.page_source
        # print(body)
        time.sleep(2)

        for m in range(4):
            try:
                time.sleep(2)
                driver.find_element_by_xpath("//button[text()='我知道了']").click()
            except Exception:
                break

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="menu-bill"]/span'))
            )
            driver.find_element_by_xpath('//*[@id="menu-bill"]/span').click()
        except Exception:
            print("点击财务管理出错")

        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//*[@id="menu-bill-submenu"]/li[1]/a'))
            )
            driver.find_element_by_xpath('//*[@id="menu-bill-submenu"]/li[1]/a').click()
        except Exception:
            print("点击我的结算单")
        if s == 0:
            Platform = "掌中云1"
        else:
            Platform = "掌中云2"
        try:
            #日期
            date_time = driver.find_element_by_xpath('//tr[1]/td[1]/a').text
            #结算金额
            General_recharge = driver.find_element_by_xpath('//tr[1]/td[7]/strong').text
            #数据入库
            insert_sql.zhangzhongyun_spider_everyday_sum(date_time,General_recharge,Platform)
        except Exception:
            print("入库失败")

        driver.quit()
if __name__=='__main__':
    zhangzhongyun_novel()


